package demo1.controller;

import demo1.dbutil.IDBUtil;
import demo1.mapper.IUserMapper;
import demo1.userinfo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start() {
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1:注册");
        System.out.println("2:登录");
        int num = scanner.nextInt();
        switch (num) {
            case 1:
                regUser();
                break;
            case 2:
                login();
                break;
            default:
                System.out.println("按个1或2这么难吗？");
        }
    }

    public void regUser() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输你的信息");
        UserInfo userInfo = new UserInfo();
        System.out.println("输入用户名：");
        userInfo.setUsername(scanner.nextLine());
        System.out.println("输入密码：");
        userInfo.setPassword(scanner.nextLine());
        System.out.println("输入昵称：");
        userInfo.setNickname(scanner.nextLine());
        System.out.println("输入年龄：");
        userInfo.setAge(scanner.nextInt());

        int num = userMapper.insert(userInfo);
        if (num > 0) {
            System.out.println("注册成功");
        } else {
            System.out.println("注册失败");
        }
    }

    public void login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输你的信息");
        UserInfo userInfo = new UserInfo();
        System.out.println("输入用户名：");
        userInfo.setUsername(scanner.nextLine());
        System.out.println("输入密码：");
        userInfo.setPassword(scanner.nextLine());

        boolean yn = userMapper.query(userInfo);
        if (yn) {
            System.out.println("登陆成功");
            System.out.println(userInfo);
        } else {
            System.out.println("登陆失败");
        }
    }
}


